package com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type;

import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.excel.core.util.ExcelUtils;
import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import com.hz.monitoring.module.hardware.controller.admin.sensor_sys_type.vo.SensorSysTypeRespVO;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type.vo.*;
import com.hz.monitoring.module.hardware.convert.sensor_sys_type.SensorSysTypeConvert;
import com.hz.monitoring.module.hardware.convert.sensor_tenant_type.SensorTenantTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_sys_type.SensorSysTypeDO;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_tenant_type.SensorTenantTypeDO;
import com.hz.monitoring.module.hardware.service.sensor_tenant_type.SensorTenantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 传感器租户分类")
@RestController
@RequestMapping("/hardware/sensor-tenant-type")
@Validated
public class SensorTenantTypeController {

    @Resource
    private SensorTenantTypeService sensorTenantTypeService;

    @PostMapping("/create")
    @ApiOperation("创建传感器租户分类")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:create')")
    public CommonResult<Long> createSensorTenantType(@Valid @RequestBody SensorTenantTypeCreateReqVO createReqVO) {
        return success(sensorTenantTypeService.createSensorTenantType(createReqVO));
    }

    @PostMapping("/create-batch")
    @ApiOperation("创建传感器租户分类")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:create')")
    public CommonResult<Boolean> createBatchSensorTenantType(@Valid @RequestBody List<SensorTenantTypeCreateReqVO> createReqVO) {
        return success(sensorTenantTypeService.createBatchSensorTenantType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新传感器租户分类")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:update')")
    public CommonResult<Boolean> updateSensorTenantType(@Valid @RequestBody SensorTenantTypeUpdateReqVO updateReqVO) {
        sensorTenantTypeService.updateSensorTenantType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除传感器租户分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:delete')")
    public CommonResult<Boolean> deleteSensorTenantType(@RequestParam("id") Long id) {
        sensorTenantTypeService.deleteSensorTenantType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得传感器租户分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:query')")
    public CommonResult<SensorTenantTypeRespVO> getSensorTenantType(@RequestParam("id") Long id) {
        SensorTenantTypeDO sensorTenantType = sensorTenantTypeService.getSensorTenantType(id);
        return success(SensorTenantTypeConvert.INSTANCE.convert(sensorTenantType));
    }

    @GetMapping("/list")
    @ApiOperation("获得传感器租户分类列表")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:query')")
    public CommonResult<List<SensorTenantTypeRespVO>> getSensorTenantTypeList(SensorTenantTypeListReqVO listReqVO) {
        List<SensorTenantTypeDO> list = sensorTenantTypeService.getSensorTenantTypeList(listReqVO);
        return success(SensorTenantTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/info-list")
    @ApiOperation("获得传感器租户分类详情列表")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:query')")
    public CommonResult<List<SensorSysTypeRespVO>> getSensorTenantTypeInfoList(SensorTenantTypeListReqVO listReqVO) {
        List<SensorSysTypeDO> list = sensorTenantTypeService.getSensorTenantTypeInfoList(listReqVO);
        return success(SensorSysTypeConvert.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得传感器租户分类分页")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:query')")
    public CommonResult<PageResult<SensorTenantTypeRespVO>> getSensorTenantTypePage(@Valid SensorTenantTypePageReqVO pageVO) {
        PageResult<SensorTenantTypeDO> pageResult = sensorTenantTypeService.getSensorTenantTypePage(pageVO);
        return success(SensorTenantTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出传感器租户分类 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-type:export')")
    @OperateLog(type = EXPORT)
    public void exportSensorTenantTypeExcel(@Valid SensorTenantTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SensorTenantTypeDO> list = sensorTenantTypeService.getSensorTenantTypeList(exportReqVO);
        // 导出 Excel
        List<SensorTenantTypeExcelVO> datas = SensorTenantTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "传感器租户分类.xls", "数据", SensorTenantTypeExcelVO.class, datas);
    }

}
